-- =============================================
-- Author:		Rosario Silva
-- Create date: 06/08/2012
-- Description:	Obtener listado de Tipo Recurso.
-- =============================================
CREATE PROCEDURE [dbo].[ResourceType_R]		
@Description NVARCHAR(50),
@Cost DECIMAL, 
@Start INT, 
@Limit INT, 
@Order NVARCHAR(50),
@Direction NVARCHAR(5)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
    -- Insert statements for procedure here
	SELECT 
	    ROW_NUMBER() OVER ( ORDER BY CASE WHEN @Order = 'Id' AND @Direction = 'ASC' THEN Id END ASC, 
	                 CASE WHEN @Order = 'Description'AND @Direction = 'ASC' THEN Descripcion END ASC,	
	                 CASE WHEN @Order = 'Cost'AND @Direction = 'ASC' THEN Costo END ASC,
	                 CASE WHEN @Order = 'Id' AND @Direction = 'DESC' THEN Id END DESC, 
	                 CASE WHEN @Order = 'Description'AND @Direction = 'DESC' THEN Descripcion END DESC,	
	                 CASE WHEN @Order = 'Cost'AND @Direction = 'DESC' THEN Costo END DESC) AS RowNum,	            
		id,
		descripcion,
		costo  
		INTO #TipoRecurso
		FROM TipoRecurso
		WHERE (descripcion LIKE '%'+@Description+'%' OR @Description = '')
		  AND (@Cost IN (Costo, 0))
		  AND activo = 1
		Select COUNT(id)AS TotalReg From #TipoRecurso
		SELECT 
			id, 
			descripcion,
			costo
			FROM    #TipoRecurso	
		WHERE   RowNum BETWEEN @Start + 1 AND (@Start + @Limit)
DROP TABLE #TipoRecurso		   
	SET NOCOUNT OFF;
END
